package com.study.homework;

public class MySortArray {

    private final int[] array;
    private int currLen;

    public MySortArray(int len) {
        array = new int[len];
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < currLen; i++) {
            sb.append(array[i])
                    .append(",");
        }
        return sb.toString();
    }

    public void add(int data) {
        if (currLen > array.length - 1) {
            throw new ArrayIndexOutOfBoundsException();
        }
        array[currLen++] = data;
    }

    public int get(int index) {
        if (index > currLen) {
            throw new ArrayIndexOutOfBoundsException();
        }
        return array[index];
    }

    public void update(int index, int data) {
        if (index >= currLen) {
            throw new ArrayIndexOutOfBoundsException();
        }
        array[index] = data;
    }

    public void remove(int index) {
        if (index >= currLen) {
            throw new ArrayIndexOutOfBoundsException();
        }
        int i = index;

        while (i < currLen - 1) {
            array[i] = array[i + 1];
            i++;
        }
        currLen--;
    }
}
